using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using DTO;
namespace Data
{
    public class PhongRepository
    {



		//////////////////////////////////////////////////////////

		                                                          

		//            Enter code your here                      //
        public PhongDTO LayPhongTheoTen(string TenPhong)
        {
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var qr = from ph in Context.PhongList
                     where ph.TenPhong == TenPhong
                     select ph;

            PhongDTO p = null;
            try
            {
                if (qr.First() != null)
                {
                    p = PhongRepository.CreatePhongDTOFromPhongEF(qr.First());
                }
            }
            catch { }
            return p;
        }

        public bool KiemTraTenTrung(int MaPhong, string TenPhong)
        {
            PhongDTO lp = this.LayPhongTheoTen(TenPhong);
            if (lp == null)
                return false;
            if (lp.MaPhong == MaPhong)
                return false;
            return true;
        }

        public List<PhongDTO> LayDanhSachPhongDaThue()
        {
            int MaTinhTrangDaThue = 1;
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.PhongList
                         orderby x.TenPhong
                         where x.MaTinhTrang == MaTinhTrangDaThue
                         select x);
            List<PhongDTO> lst = new List<PhongDTO>();
            foreach (var obj in query)
            {
                PhongDTO lp = PhongRepository.CreatePhongDTOFromPhongEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<PhongDTO> LayDanhSachPhongTheoLoai(int MaLoaiPhong)
        {
            List<PhongDTO> lst = new List<PhongDTO>();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from Phong in context.PhongList
                            where Phong.MaLoaiPhong == MaLoaiPhong
                            select Phong;
                foreach (var objEf in query)
                {
                    PhongDTO objDTO = CreatePhongDTOFromPhongEF(objEf);
                    lst.Add(objDTO);
                }
            }
            catch
            { }
            return lst;
        }

		                                                          
                                   

		//////////////////////////////////////////////////////////

        public static PhongDTO CreatePhongDTOFromPhongEF(Phong objEf) // EF -> DTO
        {
            PhongDTO objDTO = new PhongDTO();
            Mapper.CreateMap<Phong, PhongDTO>();
            objDTO = Mapper.Map<Phong, PhongDTO>(objEf);
            return objDTO;
        }

        public static Phong CreatePhongEFFromPhongDTO(PhongDTO objDTO)    //DTO -> EF
        {
            Phong objEF = new Phong();
            Mapper.CreateMap<PhongDTO, Phong>();
            objEF = Mapper.Map<PhongDTO, Phong>(objDTO);
            return objEF;
        }
       public static int Count()  //Dem so luong cac bo du lieu trong bang
        {
                QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                 int Count = (from x in Context.PhongList
                           select x.MaPhong).Count();
                 return Count;
        }
        
        public int GetIdentity() // tra ve gia tri ma moi them vo( gia tri Ma lon nhat trong Bang).
        {
            try
            {
                 QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
                  var query = from x in Context.PhongList
                        orderby x.MaPhong descending
                        select x.MaPhong;
                  var Ma = query.First();
                  return int.Parse(Ma.ToString());
            }
            catch
            {
                return 0;
            }
        }

        public bool Them(PhongDTO objDTO)
        {
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                Phong objEF = CreatePhongEFFromPhongDTO(objDTO);
                context.AddToPhongList(objEF);
                context.SaveChanges();
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

        public void CapNhat(PhongDTO objDTO)
        {
            try
            {
                Phong objEF = CreatePhongEFFromPhongDTO(objDTO);
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                context.PhongList.ApplyCurrentValues(objEF);
                context.SaveChanges();
            }
            catch (System.Exception )
            {            	
            }
        }

        public List<PhongDTO> LayDanhSach(int Start, int Record)//lay Record bo du lieu tu vi tri Start
        { 
            QuanLyKhachSanEntities Context = ContextFactory.GetInstance();
            var query = (from x in Context.PhongList
                        orderby x.MaPhong
                        select x).Skip(Start - 1).Take(Record);
            List<PhongDTO> lst = new List<PhongDTO>();
            foreach (var obj in query)
            {
                PhongDTO lp = PhongRepository.CreatePhongDTOFromPhongEF(obj);
                lst.Add(lp);
            }
            return lst;
        }

        public List<PhongDTO> LayDanhSach()//lay tat ca danh sach
        {
            List<PhongDTO> lst = new List<PhongDTO>();
            QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
            var query = from Phong in context.PhongList
                        select Phong;
            foreach (var objEf in query)
            {
                PhongDTO objDTO = CreatePhongDTOFromPhongEF(objEf);
                lst.Add(objDTO);
            }
            return lst;
        }

        public PhongDTO LayTheoMa(int MaPhong)
        {
            PhongDTO objDTO = new PhongDTO();
            try
            {
                QuanLyKhachSanEntities context = ContextFactory.GetInstance();
                var query = from Phong in context.PhongList
                            where Phong.MaPhong == MaPhong
                            select Phong;
                Phong objEF = query.First();
                objDTO = CreatePhongDTOFromPhongEF(objEF);
            }
            catch (System.Exception )
            {
            }
            return objDTO;
        }

//
//Khoa ngoai TinhTrang
//
        public List<PhongDTO> LayDanhSachTheoMaTinhTrang( int MaTinhTrang)
        {
            List<PhongDTO> lst = new List<PhongDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from Phong in context.PhongList
                         where Phong.MaTinhTrang == MaTinhTrang 
                        select Phong;
                 foreach (var objEf in query)
                 {
                      PhongDTO objDTO = CreatePhongDTOFromPhongEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaTinhTrang( int MaTinhTrang, bool XoaKhoaNgoai=false)
        {
            List<PhongDTO> lst = this.LayDanhSachTheoMaTinhTrang( MaTinhTrang);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaPhong;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }


//
//Khoa ngoai LoaiPhong
//
        public List<PhongDTO> LayDanhSachTheoMaLoaiPhong( int MaLoaiPhong)
        {
            List<PhongDTO> lst = new List<PhongDTO>();
            try
            {
                 QuanLyKhachSanEntities context =  ContextFactory.GetInstance();
                 var query = from Phong in context.PhongList
                         where Phong.MaLoaiPhong == MaLoaiPhong 
                        select Phong;
                 foreach (var objEf in query)
                 {
                      PhongDTO objDTO = CreatePhongDTOFromPhongEF(objEf);
                       lst.Add(objDTO);
                 }
            }
            catch
            {}
            return lst;
        }



        public void XoaTheoMaLoaiPhong( int MaLoaiPhong, bool XoaKhoaNgoai=false)
        {
            List<PhongDTO> lst = this.LayDanhSachTheoMaLoaiPhong( MaLoaiPhong);
            for( int i = 0; i < lst.Count; i++ )
            {
                 int iMa = lst[i].MaPhong;
                 this.Xoa(iMa, XoaKhoaNgoai);
            }
        }

        public bool Xoa(int MaPhong, bool XoaKhoaNgoai=false)
        {
            QuanLyKhachSanEntities context = new QuanLyKhachSanEntities();// ContextFactory.GetInstance();
            try
            {
                if( XoaKhoaNgoai == true)
                {
                
                       ChiTietMatDoSuDungPhongRepository ChiTietMatDoSuDungPhongRepo = new ChiTietMatDoSuDungPhongRepository();
                       ChiTietMatDoSuDungPhongRepo.XoaTheoMaPhong(MaPhong, XoaKhoaNgoai);

                       PhieuThueRepository PhieuThueRepo = new PhieuThueRepository();
                       PhieuThueRepo.XoaTheoMaPhong(MaPhong, XoaKhoaNgoai);

                }
                var query = from x in context.PhongList
                            where x.MaPhong == MaPhong
                            select x;
                if(query.First()!= null)
                {
                    Phong ef = (Phong)query.First();   
                    context.DeleteObject(ef);
                    context.SaveChanges();
                }
                return true;
            }
            catch (System.Exception )
            {
                return false;
            }
        }

////////////////////////////////////////////////////////////////

    }
}

